// 单例模式设计通话声音管理器
class CallSoundManager {
  private static instance: CallSoundManager;
  private ringtoneAudio: HTMLAudioElement | null = null;
  private callConnectAudio: HTMLAudioElement | null = null;
  private callEndAudio: HTMLAudioElement | null = null;
  private audioContext: AudioContext | null = null;
  private isRinging: boolean = false;
  private constructor() {
    // 初始化音频元素
    this.initAudios();
    // 创建音频上下文
    try {
      this.audioContext = new (window.AudioContext || (window as any).webkitAudioContext)();
    } catch (err) {
      console.error("创建AudioContext失败:", err);
    }
  }

  public static getInstance(): CallSoundManager {
    if (!CallSoundManager.instance) {
      CallSoundManager.instance = new CallSoundManager();
    }
    return CallSoundManager.instance;
  }
    private initAudios() {
      try {
        // 使用Data URL格式直接嵌入音频数据
        // 这些是简短的示例音频数据，实际应用中可能需要更长的数据
        const ringtoneDataUrl = "data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAMAAAAAAAAACoAAAA=";
        const connectSoundDataUrl = "data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAMAAAAAAAAACoAAAA=";
        const endSoundDataUrl = "data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAMAAAAAAAAACoAAAA=";
        
        // 来电铃声
        this.ringtoneAudio = new Audio(ringtoneDataUrl);
        this.ringtoneAudio.loop = true;
        this.preloadAudio(this.ringtoneAudio);

        // 通话接通音效
        this.callConnectAudio = new Audio(connectSoundDataUrl);
        this.preloadAudio(this.callConnectAudio);
        
        // 通话结束音效
        this.callEndAudio = new Audio(endSoundDataUrl);
        this.preloadAudio(this.callEndAudio);
        
        console.log("使用嵌入式音频数据");
      } catch (err) {
        console.error("初始化音频文件失败:", err);
      }
    }

  // 预加载音频
  private preloadAudio(audio: HTMLAudioElement): void {
    audio.load();
    audio.addEventListener("error", (e) => {
      console.error("音频加载错误:", e);
      console.error("音频URL:", audio.src);
    });
  }

  // 播放通话结束音效
  public playEndSound(): void {
    if (!this.callEndAudio) return;
    
    // 恢复AudioContext（如果被暂停）
    this.resumeAudioContext();
    
    try {
      // 完全重新创建一个音频实例，避免状态冲突
      const soundUrl = "data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAMAAAAAAAAACoAAAA=";
      const newAudio = new Audio(soundUrl);
      
      // 直接播放新创建的音频
      newAudio.play().then(() => {
        // 播放成功，更新音频引用
        this.callEndAudio = newAudio;
      }).catch(err => {
        console.error("播放结束音效失败:", err);
      });
    } catch (err) {
      console.error("播放结束音效错误:", err);
    }
  }

  // 播放通话接通音效
  public playConnectSound(): void {
    if (!this.callConnectAudio) return;
    
    // 恢复AudioContext（如果被暂停）
    this.resumeAudioContext();
    
    try {
      // 完全重新创建一个音频实例，避免状态冲突
      const soundUrl = "data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAMAAAAAAAAACoAAAA=";
      const newAudio = new Audio(soundUrl);
      
      // 直接播放新创建的音频
      newAudio.play().then(() => {
        // 播放成功，更新音频引用
        this.callConnectAudio = newAudio;
      }).catch(err => {
        console.error("播放接通音效失败:", err);
      });
    } catch (err) {
      console.error("播放接通音效错误:", err);
    }
  }

  // 播放来电铃声
  public playRingtone(): void {
    if (!this.ringtoneAudio) return;
    
    // 恢复AudioContext（如果被暂停）
    this.resumeAudioContext();
    
    try {
      // 如果已经在播放，不要重新开始
      if (!this.ringtoneAudio.paused) return;
      
      // 完全重新创建一个音频实例，避免状态冲突
      const ringtoneUrl = "data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAMAAAAAAAAACoAAAA=";
      const newAudio = new Audio(ringtoneUrl);
      newAudio.loop = true;
      
      // 直接播放新创建的音频
      newAudio.play().then(() => {
        // 播放成功，更新音频引用
        this.ringtoneAudio = newAudio;
        this.isRinging = true;
      }).catch(err => {
        console.error("播放铃声失败:", err);
        this.isRinging = false;
      });
    } catch (err) {
      console.error("播放铃声错误:", err);
      this.isRinging = false;
    }
  }

  // 停止来电铃声
  public stopRingtone(): void {
    if (this.ringtoneAudio && !this.ringtoneAudio.paused) {
      // 创建一个新的空音频元素，避免对正在播放的元素调用pause()
      this.ringtoneAudio = new Audio();
      this.isRinging = false;
    }
  }

  // 恢复音频上下文
  private resumeAudioContext(): void {
    if (this.audioContext && this.audioContext.state === "suspended") {
      this.audioContext.resume().catch(err => {
        console.error("恢复AudioContext失败:", err);
      });
    }
  }
}

export default CallSoundManager; 